from PyQt4.QtGui import QWidget
from PyQt4 import QtCore,QtGui
from KCharts.KPlotItem.KChartPriceDisplayItem import KChartPriceDisplayItem
from KCharts.KPlotItem.ui_KSingleChartPlot import Ui_KSingleChartPlot
from KCharts.KPlotItem.KChartViewBox import KChartViewBox
from KCharts.KPlotItem.KChartViewBoxMenu import KChartViewBoxMenu
from KCharts.KPlotItem.KChartAxisItem import KChartAxisItem

import pyqtgraph as pg
from pyqtgraph.dockarea import *
import numpy as np
class KSingleChartPlot(QWidget):
	'''
	
	'''
	PeriodChanged=QtCore.pyqtSignal(str)
	
	def __init__(self, parent,stockobject):
		super(KSingleChartPlot, self).__init__(parent)
		self.ui =  Ui_KSingleChartPlot()
		self.ui.setupUi(self) 
		self.ui.verticalLayout.setContentsMargins(0,0,0,0) # set blank space between outside of layout and its parent widget		
		self.ui.verticalLayout.setSpacing(0)
		
		self.m_stock=stockobject
		self.axis=KChartAxisItem(orientation='bottom')
		self.axis.Initialize(stockobject)	
		
		self.mviewbox=KChartViewBox()
		self.mviewbox.PeriodChanged.connect(self.PeriodChanged)
		
		self.plot=self.ui.graphicsView.addPlot(1,1,1,1,viewBox=self.mviewbox, axisItems={'bottom': self.axis}, enableMenu=True) 
		self.plot.setMenuEnabled(False, None)
		self.plot.showGrid(True,True,0.5)
		
		#cross hair
		self.vLine = pg.InfiniteLine(angle=90, movable=False)
		self.hLine = pg.InfiniteLine(angle=0, movable=False)
		self.label = pg.LabelItem(justify='right')
		self.plot.addItem(self.vLine, ignoreBounds=True)
		self.plot.addItem(self.hLine, ignoreBounds=True)
		self.ui.graphicsView.addItem(self.label)
		
		self.proxy = pg.SignalProxy(self.plot.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)

		self.UpdateDisplay()
	def __del__(self):
		if self.proxy:
			self.proxy.disconnect()
			
	def UpdateDisplay(self):
		
		self.axis.UpdateDisplay()
		#curindex=self.m_stock.CurWindowData('Index')
		#self.mviewbox.setRange(xRange=[curindex[0],curindex[-1]])
		for each_item in self.mviewbox.addedItems:
			each_item.UpdateDisplay()
	
	def mouseMoved(self,evt):
		# triggered when mouse move on the interface
		pos = evt[0]  ## using signal proxy turns original arguments into a tuple
		mstock=self.m_stock
		if self.plot.sceneBoundingRect().contains(pos):
			mousePoint = self.mviewbox.mapSceneToView(pos)
			index = int(round(mousePoint.x()))
			mindexlist=mstock.CurWindowData('Index')
			if index >= mindexlist[0] and index <=mindexlist[-1]:
				if index in mindexlist:
					actualindex=mindexlist.index(index)
				Time=mstock.CurWindowData('Time')[actualindex]
				Price=mstock.CurWindowData('Price')	[actualindex]			
				self.label.setText(self.GetLabel(Time,Price))
				#label.setText("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: red'>y1=%0.1f</span>,   <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))
			self.vLine.setPos(mousePoint.x())
			self.hLine.setPos(mousePoint.y())
			
	def GetLabel(self,time,priceblock):
		result='Date: %s Open: %.2f High: %.2f Low: %.2f Close: %.2f' %(time,priceblock[0],priceblock[1],priceblock[2],priceblock[3])
		result="<span style='font-size: 11pt; color: white'>%s</span>" %(result)
		return result
		

